package berzinga;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import justcommunicate.SocketClient;
import justcommunicate.SocketServer;
import justcommunicate.TextReciever;

public class MessagePanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7461516738851660587L;
	private static final int WIDTH = 800;
	private static final int HIGTH = 600;
	private Frame f;
	private Button b1;
	private Button b2;
	private JTextArea textToSent;
	private JTextArea recievedField;
	private JPanel sf;
	TextReciever tr;
	public static void main(String[] args) {
		MessagePanel j = new MessagePanel();
		j.go();
	}
	
	public void go() {
		// create a instance of the class java.awt.Frame
		GridLayout experimentLayout = new GridLayout(3,1);
		f = new JFrame("GUI example");
		f.setLayout(experimentLayout);
		f.setSize(WIDTH, HIGTH);
		// create a instance of flow layout manager
		// and install it in the Frame
		textToSent = new JTextArea();
		textToSent.setSize(50, 20);
		recievedField = new JTextArea("Recieve Box");
		recievedField.setSize(50, 20);

		tr = new TextReciever(recievedField);
		// create a instance of class java.awt.Button
		b1 = new Button("send");
		b1.addActionListener(new ClickListener());
		// the button label is defined by the String argument
		// to the constructor
		f.add(textToSent);
		f.add(b1);
		f.add(recievedField);
		
		f.setVisible(true);
		
		Thread t = new Thread(tr);
		t.run();
	}
	
	private class ClickListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			recievedField.setText(recievedField.getText() + "\r\n" + "I said: " + textToSent.getText());
			SocketClient s = new SocketClient();
			s.send(SocketServer.SERVER_IP, SocketServer.SERVER_PORT, textToSent.getText().getBytes());
			textToSent.setText("");
		}

	  }
}
